//
//  MZTool.m
//  民族文化宫
//
//  Created by kobehjk on 14-7-29.
//  Copyright (c) 2014年 WANG CUNRUI. All rights reserved.
//

#import "MZTool.h"

@implementation MZTool
+ (BOOL) IsEnableWIFI
{
    return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}


+ (BOOL) IsEnable3G
{
    NSLog(@"3g%d",([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable));
    return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
    
}


// 是否达到了时间间隔需要刷新
+ (BOOL) IsNeedUpdate:(NSString *)update
{
    //获取上一次数据更新时间
    NSTimeZone *fromzone = [NSTimeZone systemTimeZone];
    NSInteger frominterval = [fromzone secondsFromGMTForDate:[[NSUserDefaults standardUserDefaults] objectForKey:update]];
    NSDate *fromDate = [[[NSUserDefaults standardUserDefaults] objectForKey:update] dateByAddingTimeInterval:frominterval];
    NSLog(@"fromdate=%@",fromDate);
    
    //获取当前时间
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:date];
    NSDate *localeDate = [date dateByAddingTimeInterval:interval];
    NSLog(@"enddate=%@",localeDate);
    
    double intervalTime = [localeDate timeIntervalSinceReferenceDate] - [fromDate timeIntervalSinceReferenceDate];
    long lTime = (long)intervalTime;
    NSInteger iSeconds = lTime % 60;
    NSInteger iMinutes = (lTime / 60) % 60;
    NSInteger iHours = (lTime / 3600);
    NSInteger iDays = lTime/60/60/24;
    NSInteger iMonth = lTime/60/60/24/12;
    NSInteger iYears = lTime/60/60/24/384;
    NSLog(@"距离上一次刷新时间相差%d年%d月%d日%d小时%d分钟%d秒",iYears,iMonth,iDays,iHours,iMinutes,iSeconds);
    
    if ([update isEqualToString:@"HomePageLastUpdate"])
    {
        if (iMinutes > 59 || iHours > 59 || iDays > 1 || iMonth > 0 || iYears > 0)
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }
    else if ([update isEqualToString:@"ZhanlanLastUpdate"])
    {
        if (iMinutes > 59 || iHours > 59 || iDays > 1 || iMonth > 0 || iYears > 0)
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }
    else if ([update isEqualToString:@"MianjuLastUpdate"])
    {
        if (iMinutes > 59 || iHours > 59 || iDays > 1 || iMonth > 0 || iYears > 0)
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }
    else if ([update isEqualToString:@"DiancangLastUpdate"])
    {
        if (iMinutes > 59 || iHours > 59 || iDays > 1 || iMonth > 0 || iYears > 0)
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }
    else if ([update isEqualToString:@"FushiLastUpdate"])
    {
        if (iMinutes > 59 || iHours > 59 || iDays > 1 || iMonth > 0 || iYears > 0)
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }
    else if ([update isEqualToString:@"TangkaLastUpdate"])
    {
        if (iMinutes > 59 || iHours > 59 || iDays > 1 || iMonth > 0 || iYears > 0)
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }
    else if ([update isEqualToString:@"ShuhuaLastUpdate"])
    {
        if (iMinutes > 59 || iHours > 59 || iDays > 1 || iMonth > 0 || iYears > 0)
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }
    else if ([update isEqualToString:@"JieshaoLastUpdate"])
    {
        if (iMinutes > 59 || iHours > 59 || iDays > 1 || iMonth > 0 || iYears > 0)
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }
    else
    {
        return NO;
    }
}

+ (NSString*)deviceString
{
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    
    if ([deviceString isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
    if ([deviceString isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
    if ([deviceString isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    if ([deviceString isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    if ([deviceString isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([deviceString isEqualToString:@"iPhone5,2"])    return @"iPhone 5";
    if ([deviceString isEqualToString:@"iPhone3,2"])    return @"Verizon iPhone 4";
    if ([deviceString isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
    if ([deviceString isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
    if ([deviceString isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
    if ([deviceString isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
    if ([deviceString isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([deviceString isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([deviceString isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
    if ([deviceString isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([deviceString isEqualToString:@"i386"])         return @"Simulator";
    if ([deviceString isEqualToString:@"x86_64"])       return @"Simulator";
    NSLog(@"NOTE: Unknown device type: %@", deviceString);
    return deviceString;
}

@end
